package com.renbang.action;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.renbang.model.common.AjaxResult;
import com.renbang.model.common.Page;
import com.renbang.model.sys.SysUser;
import com.renbang.service.BaseServicesImp;

@Controller
@RequestMapping(value = "/")

/**
 * 
 * @description 共通Action所有的action只要继承这个类就自动有了分页查询和删除功能
 * @author chen
 * @date 2017年4月28日
 */
public abstract class BaseAction <T>{
	@Autowired
	BaseServicesImp<T> BaseServicesImp;
	
	@RequestMapping(value = "/showList")
	public  @ResponseBody AjaxResult showList(HttpServletRequest request,Page page,T obj) {
		Integer count = BaseServicesImp.findTotal(obj);
		return new AjaxResult(AjaxResult.STATUS_OK, page.getCurrpage()+"","数据加载成功" ,BaseServicesImp.findInPage(obj,page), count);
	}
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(Integer id) {
		Integer num = BaseServicesImp.removeById(id);
		return new AjaxResult(AjaxResult.STATUS_OK, "成功删除"+num+"条");
	}
	
	/**
	 * 
	 * @Description:拿到登录用户对象
	 * @author:user
	 * @param BaseAction.java
	 * @return SysUser
	 * @date 2017年5月10日
	 */
	public SysUser getUser() {
		return (SysUser) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession().getAttribute("user");
	
	}
}
